package com.link.base.base.bugmanagement.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.user.model.User;
import com.link.base.base.bugmanagement.model.BugManagement;
import com.link.base.base.bugmanagement.service.BugManagementService;
import com.link.base.base.serviceprojectnew.model.Project;
import com.link.base.base.serviceprojectnew.model.ProjectTeam;
import com.link.base.base.serviceprojectnew.service.ProjectTeamService;
import com.link.base.base.user.service.UserService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright  hande Company. All Rights Reserved.
 * BUG管理
 *
 * @author 阳晓琳
 * @version 1.0
 * @date: 2018/11/8 16:30
 */
@Controller
@RequestMapping("/link/bugManagement")
public class BugManagementController extends BasicController<BugManagement> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private BugManagementService bugManagementService;

    @Resource
    private ProjectTeamService projectTeamService;

    @Resource
    private UserService userService;

    @Override
    public BasicService<BugManagement> getBasicService() throws Exception {
        return bugManagementService;
    }

    /**
     * 赋默认值
     *
     * @param qps
     * @param session
     * @param request
     * @return
     */
    @RequestMapping("/newBugManageInit")
    @ResponseBody
    public Map<String, Object> newDealerInit(@JsonParam QueryParams qps, HttpSession session, HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        CoreUser user = UserUtil.getUser();
        try {
            BugManagement record = new BugManagement();
            Long id = keyGenerateService.keyGenerate();
            record.setId(id);
            Long userId = user.getId();
            String userName = user.getFirstName();
            Long sOrgid = user.getOrgId();
            String sOrgName = user.getOrgName();
            Long sPostnId = user.getPostnId();
            String sPostnName = user.getPostnName();
            record.setOrgId(sOrgid);
            record.setOrgName(sOrgName);
            record.setPostnId(sPostnId);
            record.setPostnName(sPostnName);
            record.setCreater(userName);
            record.setCreatedBy(userId);
            record.setStatus("new");
            record.setReproSteps("<p style='color:green'>【重现步骤】</p>"
                    + "<p><br/></p><p style='color:green'>【预期效果】</p><p><br/></p><p style='color:green'>【实际效果】<br/></p><p><br/></p>");
            result.put("bugManage", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getLocalizedMessage());
        }
        return result;
    }

    /**
     * 获取该项目的开发人员
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/developerList")
    @ResponseBody
    public Map<String, Object> queryDevelopersList(@JsonParam QueryParams qps, @JsonParam BugManagement entity,
                                                   HttpSession session, HttpServletRequest request,
                                                   HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.invokePreFilterAndSorter();
            qps.preDealSecurity(request);
            qps.setOauth("ALL");
            Project project = (Project) BasicModel.transformClass(Project.class, qps);
            String projCode = request.getParameter("projCode");
            project.setProjCode(projCode);
            List<ProjectTeam> list = projectTeamService.queryBugDevelopersListPage(project);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(((BasicModel) project), list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 获取当前账套的所有用户
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/corpUserList")
    @ResponseBody
    public Map<String, Object> queryCorpUserList(@JsonParam QueryParams qps, @JsonParam BugManagement entity,
                                                 HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.invokePreFilterAndSorter();
            qps.preDealSecurity(request);
            qps.setOauth("ALL");
            User user = (User) BasicModel.transformClass(User.class, qps);
            List<User> list = userService.queryByExamplePage(user);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(((BasicModel) user), list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 列表页更新行记录
     *
     * @param entity
     * @return
     */
    @RequestMapping("/updateByList")
    @ResponseBody
    public Map<String, Object> updateByList(@JsonParam BugManagement entity) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (null == entity.getId()) {
                result.put("success", false);
                result.put("result", "BUG管理id不能为空");
                return result;
            }
            BugManagement record = bugManagementService.updateByList(entity);
            result.put("newRow", record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getLocalizedMessage());
        }
        return result;
    }
}
